const reqlib = require('app-root-path').require
const numberUtil = reqlib('/app/util/numberUtil')

const getNumberVal = (val) => {
  return parseFloat(val) || 0
}

module.exports = {
  getVolumeEnlargeRate (emaInfo) {
    const emInfoList = [...(emaInfo.emInfoList || [])]
    emInfoList.reverse()
    // 近的在前
    const avVol = (getNumberVal(emInfoList[1].volume) + getNumberVal(emInfoList[2].volume) + getNumberVal(emInfoList[3].volume) + getNumberVal(emInfoList[4].volume)) / 4
    return {
      last: getNumberVal(emInfoList[0].volume) / (getNumberVal(emInfoList[1].volume) || 1),
      av: getNumberVal(emInfoList[0].volume) / (avVol || 1)
    }
  },
  isVolumeEnlarge (emaInfo) {
    const rate = this.getVolumeEnlargeRate(emaInfo)
    return rate.last > 9 && rate.av > 6
  },
  getRealAmplitudeRate (emaInfo) {
    const max = Math.abs(numberUtil.countDifferenceRate(emaInfo.high, emaInfo.low))
    const real = Math.abs(numberUtil.countDifferenceRate(emaInfo.close, emaInfo.open))
    return real / max
  }
}
